import { Node } from "cc"
import { KugoPiecesType } from "../KugoConst"
import { ChessPoint, GameState } from "../proto/proto"

export interface Pieces {
    type:KugoPiecesType
    node?:Node
}
/**久棋棋盘模型 */
export class KugoChessDataModel {
    private _gridSize = 0

    constructor(gridSize){
        this.initBase(gridSize)
    }


    /**棋盘数据 初始都为0*/
    private _chessBase: Pieces[][] = []
    /**棋盘数据 初始都为0*/
    get ChessBaseData(){
        return this._chessBase
    }
    /**初始化棋盘 */
    private initBase(gridSize){
        this._gridSize = gridSize
        this.Reset()
    }

    /**重置棋盘 */
    Reset(){
        for (let row = 0; row < this._gridSize; row++) {
            this._chessBase[row] = []
            for (let col = 0; col < this._gridSize; col++) {
                this._chessBase[row][col] = {
                    type: KugoPiecesType.NONE,
                    node: null
                }
            }
        }
    }

    /**网络重连初始化棋盘 */
    reconectInit(chessPieces: KugoPiecesType[][]){
        for (let row = 0; row < this._gridSize; row++) {
            this._chessBase[row] = this._chessBase[row] || []
            for (let col = 0; col < this._gridSize; col++) {
                if(this._chessBase[row][col]){
                    this._chessBase[row][col].type = chessPieces[row][col]
                }else{
                    this._chessBase[row][col] = {
                        type: chessPieces[row][col],
                        node: null
                    }
                }
                
            }
        }
    }

    CheckDrop(point:ChessPoint){
        return this._chessBase[point.Row][point.Col].type == KugoPiecesType.NONE
    }
    /**
     * 落子
     * @param row 行
     * @param col 列
     */
    Drop(point:ChessPoint, piecesType: KugoPiecesType, pieces:Node){
        this._chessBase[point.Row][point.Col].type = piecesType
        this._chessBase[point.Row][point.Col].node = pieces
    }

    DelPieces(point:ChessPoint){
        this._chessBase[point.Row][point.Col].type = KugoPiecesType.NONE
        let pieces = this._chessBase[point.Row][point.Col].node
        this._chessBase[point.Row][point.Col].node = null
        return pieces
    }
    
    setPieces(point:ChessPoint,pieces :Node){
        this._chessBase[point.Row][point.Col].node = pieces
    }


    private _curOptSeatId:number = null
    get CurSeat(){
        return this._curOptSeatId
    }

    setCurSeat(seatId:number){
        this._curOptSeatId = seatId
    }

    private _gameState:GameState = GameState.Wait
    setGameState(state:GameState){
        this._gameState = state
    }

    get GameState(){
        return this._gameState
    }

    getStateStr(){
        if (this._gameState == GameState.Wait) {
            return "准备阶段"
        }else if (this._gameState == GameState.DropPieces) {
            return "摆棋阶段"
        }else if (this._gameState == GameState.Jump) {
            return "挑起阶段"
        }else if (this._gameState == GameState.Fly) {
            return "飞棋阶段"
        }else if (this._gameState == GameState.GameOver) {
            return "已结束"
        }
    }
}